package com.example.springboot_jap_model.dao;

import com.example.springboot_jap_model.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserDao extends JpaRepository<User, Long> {

    /**
     * 根据用户名查询
     * @param userName
     * @return
     */
    List<User> findByUserName(String userName);

    /**
     * 根据 用户名 和用户状态查询
     *
     * @param userName
     * @param status
     * @return
     */
    List<User> findByUserNameAndStatus(String userName,Integer status);

    List<User> findByStatusAndType(Integer status, Integer type);

    List<User> findByType(Integer type);

    @Query(value = "SELECT uar.*  from user_and_role as uar  " +
            "INNER JOIN  " +
            "(SELECT raa.*  from role_and_authority as raa  INNER JOIN  authority as au  " +
            "on raa.auth_id = au.id  where au.url = ?2 ) as abc  " +
            "on uar.role_id = abc.role_id  " +
            "where uar.user_id = ?1  ", nativeQuery = true)
    List findAutoByUserIdAndUrl(Long userId, String url);

    /**
     * 根据 条件分页查询
     * @param type
     * @param status
     * @param pageable
     * @return
     */
    Page<User> findByTypeAndStatus(Integer type, Integer status, Pageable pageable);
}
